package com.walterweimod.auxiliary_attributes.mobeffect;

import com.walterweimod.auxiliary_attributes.attribute.ModAttributes;
import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeDefaultValueBase;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeRangedValueBase;
import com.walterweimod.auxiliary_attributes.util.ModAttributeUtils;
import com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeMap;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import org.jetbrains.annotations.NotNull;

import java.util.Map;
import java.util.UUID;

import static com.walterweimod.auxiliary_attributes.util.ModAttributeUtils.getNameAttributeUUID;
import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.*;
import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.getCapabilityValue;

public class Tenacity extends MobEffect {
    private static final UUID MAX_TENACITY_UUID = getNameAttributeUUID(
            ModAttributes.MAX_TENACITY.get(),
            "tenacity"
    );

    protected Tenacity() {
        super(MobEffectCategory.BENEFICIAL, 0xFF00FF00);
    }

    @Override
    public @NotNull Component getDisplayName() {
        return new TextComponent("Tenacity");
    }

    @Override
    public void addAttributeModifiers(@NotNull LivingEntity p_19478_,
                                      @NotNull AttributeMap p_19479_,
                                      int  p_19480_) {
        ModAttributeUtils.addAttributeModifier(
                p_19478_,
                ModAttributes.MAX_TENACITY.get(),
                1.0D + 0.35 *  p_19480_,
                MAX_TENACITY_UUID,
                AttributeModifier.Operation.ADDITION,
                false
        );

        ModCapabilityUtils.setCapabilityValue(
                p_19478_,
                ModCapabilities.TENACITY_CHARGE_VALUE,
                tenacityChargeValue -> {
                    tenacityChargeValue.setLivingEntity(p_19478_);
                    tenacityChargeValue.setAttributeMap(Map.of(
                            "max", ModAttributes.MAX_TENACITY.get()
                    ));
                    tenacityChargeValue.setValue(0.0F);
                }
        );

        if (!p_19478_.level.isClientSide()){
            getLogger().info("戒律性闪变初始化成功，初始值为：{}", getCapabilityValue(
                    p_19478_,
                    ModCapabilities.TENACITY_CHARGE_VALUE,
                    ChargeRangedValueBase::getValue,
                    0.0F
            ));

            getLogger().info("戒律性闪变初始化成功，最大值为：{}", getCapabilityValue(
                    p_19478_,
                    ModCapabilities.TENACITY_CHARGE_VALUE,
                    ChargeRangedValueBase::getMaxValue,
                    0.0F
            ));
        }

        super.addAttributeModifiers(p_19478_, p_19479_,  p_19480_);
    }

    @Override
    public void removeAttributeModifiers(@NotNull LivingEntity p_19469_,
                                         @NotNull AttributeMap p_19470_,
                                         int p_19471_) {
        ModAttributeUtils.removeAttributeModifier(
                p_19469_,
                ModAttributes.MAX_TENACITY.get(),
                MAX_TENACITY_UUID
        );

        ModCapabilityUtils.setCapabilityValue(
                p_19469_,
                ModCapabilities.TENACITY_CHARGE_VALUE,
                tenacityChargeValue -> {
                    tenacityChargeValue.setMaxValue(0.0F);
                    tenacityChargeValue.setValue(0.0F);
                }
        );

        if (!p_19469_.level.isClientSide()){
            getLogger().info("戒律性闪变移除成功");
        }

        super.removeAttributeModifiers(p_19469_, p_19470_, p_19471_);
    }

    @Override
    public boolean isDurationEffectTick(int p_19455_, int p_19456_) {
        return p_19455_ % 600 == 0;
    }

    @Override
    public void applyEffectTick(@NotNull LivingEntity p_19467_, int p_19468_) {
        ModCapabilityUtils.setCapabilityValue(
                p_19467_,
                ModCapabilities.TENACITY_CHARGE_VALUE,
                ChargeDefaultValueBase::resetValue
        );

        if (!p_19467_.level.isClientSide()){
            getLogger().info("戒律性闪变重置值成功");
        }

        super.applyEffectTick(p_19467_, p_19468_);
    }
}
